﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace LHWebApp
{
    /// <summary>
    ///ServicesVehicleAlarmInfo 的摘要说明
    /// </summary>
    public class ServicesVehicleAlarmInfo
    {
        public ServicesVehicleAlarmInfo()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 查找报警信息
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="sort"></param>
        /// <param name="dir"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="AlarmType"></param>
        /// <param name="Flag"></param>
        /// <param name="LicensePlate"></param>
        /// <returns></returns>
        public static DataSet GetAlarms(int start, int limit, string sort, string dir, string StartTime, string EndTime, int AlarmType, int Flag, string LicensePlate)
        {
            DataSet ds = new DataSet("Article");

            // 从数据库获取数据，放入 record 表
            string strSql = string.Empty;

            strSql = "select top 500 a.License_Plate,b.VID,b.Alarm_Time,b.Alarm_Location,b.Alarm_Type,b.FLAG,b.OPERATOR,b.DISPOSE_TIME,b.ROWID,b.Disposition,b.LONGITUDE,b.LATITUDE from vehicle a,DISPOSEALARM b where a.VID=b.VID and b.Alarm_Time between '";

            strSql = strSql + StartTime + "' and '" + EndTime + "' ";

            if (AlarmType != -1)
            {
                if (AlarmType == 2)
                {
                    strSql = strSql + " and b.Alarm_Type > 20 and b.Alarm_Type < 29";//目前的紧急报警是21到28
                }
                else
                {
                    strSql = strSql + " and b.Alarm_Type=" + AlarmType;
                }

            }
            if (Flag != -1)
            {
                strSql = strSql + " and b.FLAG=" + Flag;
            }
            if (LicensePlate != "")
            {
                strSql = strSql + " and a.License_Plate like  '%" + LicensePlate + "%' ";
            }
            //if (Vid != -1)
            //{
            //    Sql = Sql + " and a.vid like  '%" + condition.Vid.ToString() + "%' ";
            //}
            strSql = strSql + " order by b.Alarm_Time desc";

            //strSql = string.Format(
            //    "select top {0} a.License_Plate,b.Alarm_Type,b.Alarm_Location,b.VID,b.Alarm_Time,b.FLAG,b.OPERATOR,b.DISPOSE_TIME,b.ROWID,b.Disposition,b.LONGITUDE,b.LATITUDE  from vehicle a,DISPOSEALARM b where a.VID=b.VID and Alarm_Time not in "
            //    + "(select top {1} Alarm_Time from vehicle a,DISPOSEALARM b where a.VID=b.VID order by {2} {3}) order by {2} {3}",
            //    limit, start, sort, dir);

            SqlDataAdapter da = new SqlDataAdapter(strSql, Services.ConnectionString);
            DataTable dtRecord = new DataTable("record");
            lock (da)
            {
                da.Fill(dtRecord);
            }
            ds.Tables.Add(dtRecord);

            // 计算总行数，放入 results 表
            DataTable dtResult = new DataTable("results");
            dtResult.Columns.Add("totalRecords");
            DataRow dr = dtResult.NewRow();

            using (SqlConnection conn = new SqlConnection(Services.ConnectionString))
            using (SqlCommand cmd = new SqlCommand(strSql, conn))
            {
                try
                {
                    conn.Open();
                    dr["totalRecords"] = (int)cmd.ExecuteScalar();
                }
                catch
                {
                    // do nothing
                }
            }
            dtResult.Rows.Add(dr);
            ds.Tables.Add(dtResult);

            return ds;
        }

        //报警信息 列表
        public static DataSet GetAlarmList(string clientNo, string currentService)
        {
            DataSet ds = new DataSet("Article");

            // 从数据库获取数据，放入 record 表
            string strSql = string.Empty;
            strSql = string.Format("Exec GetAlarmList '{0}'", clientNo);
            if (currentService == "3")
            {
                strSql = string.Format("Exec GetAlarmListRubbish '{0}'", clientNo);
            }
            if (currentService == "5")
            {
                strSql = string.Format("Exec WebGetAlarmListAll '{0}'", clientNo);
            }

            SqlDataAdapter da = new SqlDataAdapter(strSql, Services.ConnectionString);
            DataTable dtRecord = new DataTable("record");
            lock (da)
            {
                da.Fill(dtRecord);
            }
            ds.Tables.Add(dtRecord);
            return ds;
        }

        //报警类型
        public static DataSet GetAlarmAllType()
        {
            DataSet ds = new DataSet("Article");

            // 从数据库获取数据，放入 record 表
            string strSql = string.Format("Exec WebGetAlarmAllType");
           
            SqlDataAdapter da = new SqlDataAdapter(strSql, Services.ConnectionString);
            DataTable dtRecord = new DataTable("record");
            lock (da)
            {
                da.Fill(dtRecord);
            }
            ds.Tables.Add(dtRecord);
            return ds;
        }

        //报警查询 结果列表
        public static DataSet GetAlarmAllSelect(string clientNo, string type, string StartTime, string EndTime, string AlarmType, string Flag, string LicensePlate)
        {
            DataSet ds = new DataSet("Article");

            // 从数据库获取数据，放入 record 表
            //WebGetAlarmAllSelect  '0000004','全部','全部','全部','2018','2020'
            if (type == "1") {
                type = "机扫车";
            }
            if (type == "2")
            {
                type = "环卫工";
            }
            if (type == "3")
            {
                type = "垃圾车";
            }
            if (type == "5")
            {
                type = "全部";
            }
            string strSql = string.Format("Exec WebGetAlarmAllSelect '{0}','{1}','{2}','{3}','{4}','{5}'", clientNo, type, LicensePlate, AlarmType, StartTime, EndTime);

            SqlDataAdapter da = new SqlDataAdapter(strSql, Services.ConnectionString);
            DataTable dtRecord = new DataTable("record");
            lock (da)
            {
                da.Fill(dtRecord);
            }
            ds.Tables.Add(dtRecord);
            return ds;
        }

        internal static bool AlarmInfoDelBatch(string alarmIDList)
        {
            try
            {
                string sql = string.Format("Exec AlarmInfoDelBatch '{0}'", alarmIDList);
                Services.SetData(sql);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        internal static bool AlarmInfoConfirmBatch(string alarmIDList, string operators, string disposition)
        {
            try
            {
                string sql = string.Format("Exec AlarmInfoConfirmBatch '{0}','{1}','{2}'", alarmIDList, operators, disposition);
                Services.SetData(sql);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}